package fun.wlfj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import fun.wlfj.dao.IPayListDao;
import fun.wlfj.dbc.IConnectionManager;
import fun.wlfj.dbc.MySQLConnection;
import fun.wlfj.vo.PayList;
import fun.wlfj.vo.Product;
import fun.wlfj.vo.User;

public class PayListDaoImpl extends AbsDaoImpl implements IPayListDao {
	
	private User user;

	public PayListDaoImpl(Connection conn, User user) {
		super(conn);
		this.user = user;
	}

	
	/**
	 * 为当前用户 创建一个新订单 并将id返回
	 */
	@Override
	public int makePayList() {
		int PayListID = -1;
		try {
			// Bad smelling.
			PreparedStatement pstsm_t = conn.prepareStatement("select UserID from User where UserName = ?;");
			pstsm_t.setString(1, user.getUserName());
			ResultSet rs1 = pstsm_t.executeQuery();
			rs1.next();
			user.setUserID(rs1.getInt(1));
			rs1.close();
			pstsm_t.close();
			// Bad smelling.
			PreparedStatement pstsm = conn.prepareStatement("insert into PayList(UserID) values(?);");
			pstsm.setInt(1, user.getUserID());//这里需要得到用户ID!
			if(pstsm.executeUpdate() == 1) {
				PreparedStatement pstsm1 = conn.prepareStatement("select max(PayListID) from PayList;");
				ResultSet rs = pstsm1.executeQuery();
				rs.next();
				PayListID = rs.getInt(1);
				IConnectionManager.closePstsm(pstsm1);
			}else {
				PayListID = -1;
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return PayListID;
	}

	@Override
	public PayList getPayListByID(int userID, int payListID) {
		PayList pl = new PayList(userID, payListID, new LinkedList<Product>());
		try {
			PreparedStatement pstsm = conn.prepareStatement("select Date, PayCash from PayList where PayListID = ?;");
			pstsm.setInt(1, payListID);
			ResultSet rs1 = pstsm.executeQuery();
			rs1.next();
			pl.setPayDate(rs1.getDate(1));
			pl.setPayCash(rs1.getDouble(2));
			pl.getProducts().addAll(getProduct(pl));
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pl;
	}

	@Override
	public List<PayList> getPayLists(int type) {
		List<PayList> list = new LinkedList<PayList>();
		// 现在我们要解析我们需要的类型 再将其全部放入
		if((type & 1) == 1) {
			// 需要未支付的
			list.addAll(getList("select PayListID, UserID from unpayorder;"));
		}
		if(((type & (1 << 1)) == (1 << 1))) {
			// 需要已支付的
			list.addAll(getList("select PayListID, UserID from finishedorder;"));
		}
		if(((type & (1 << 2)) == (1 << 2))) {
			// 需要已取消的
			list.addAll(getList("select PayListID, UserID from cancledorder;"));
		}
		return list;
	}
	
	private List<PayList> getList(String sql){
		List<PayList> list = new LinkedList<PayList>();
		try {
			PreparedStatement pstsm = conn.prepareStatement(sql);
			ResultSet rs = pstsm.executeQuery();
			while(rs.next()) {
				list.add(getPayListByID(rs.getInt("UserID"), rs.getInt("PayListID")));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<PayList> getPayLists(int type, Date date) {
		List<PayList> list = getPayLists(type);
		List<PayList> list1 = new LinkedList<PayList>();
		for(PayList p : list) {
			if(p.getPayDate().getDay() != date.getDay()) {
				//list.remove(p);
			}else {
				list1.add(p);
			}
		}
		return list1;
	}

	@Override
	public String addProduct(PayList pl, Product p) {
		String msg = "";
		try {
			PreparedStatement pstsm = conn.prepareStatement("insert into PayDetail(PayListID, ProductID, Count) values(?, ?, ?);");
			pstsm.setInt(1, pl.getPayListID());
			pstsm.setInt(2, p.getProductID());
			pstsm.setInt(3, p.getCount());
			if(pstsm.executeUpdate() == 1) {
				msg = "添加成功！";
			}else {
				msg = "添加失败！";
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			msg = "库存不足！";
			e.printStackTrace();
		}
		return msg;
	}

	@Override
	public List<Product> getProduct(PayList pl) {
		List<Product> list = new LinkedList<Product>();
		try {
			PreparedStatement pstsm = conn.prepareStatement("select ProductID, ProductNums, ProductName, ProductShortName, Count, ProductPrice, Sum  from userpaylistproducts where PayListID = ?;");
			pstsm.setInt(1, pl.getPayListID());
			ResultSet rs = pstsm.executeQuery();
			while(rs.next()) {
				list.add(new Product(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getDouble(6), rs.getDouble(7)));
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public String removeProduct(PayList pl, Product p) {
		String msg = null;
		try {
			PreparedStatement pstsm = conn.prepareStatement("delete from PayDetail where PayListID = ? and ProductID = ?");
			pstsm.setInt(1, pl.getPayListID());
			pstsm.setInt(2, p.getProductID());
			if(pstsm.executeUpdate() == 1) {
				msg = "删除成功！";
			}else {
				msg = "删除失败！";
			}
			IConnectionManager.closePstsm(pstsm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msg;
	}
	
	public static void main(String[] args) {
		// 现在我们要测试一下
		/**
		 * makePayList -- ok
		 * removeProduct
		 * getProduct -- ok
		 * addProduct -- ok
		 * getPayLists -- ok
		 * getPayListByID -- ok
		 */
		
		IConnectionManager manager = new MySQLConnection();
		IPayListDao test = new PayListDaoImpl(manager.getConn(), new User(4, null, null, null, null));
//		int PayListID = test.makePayList();
//		test.addProduct(new PayList(2, PayListID, null), new Product(4,4));
//		PayList pl = test.getPayListByID(1, PayListID);
//		System.out.println("得到测试订单：" + pl.toString());
//		System.out.println("测试用户买了以下物品");
//		for(Product product : pl.getProducts()) {
//			System.out.println(product);
//		}
		List<PayList> payLists = test.getPayLists(7, new Date());
		System.out.println("以下是其全部订单：" + new Date());
		for(PayList p : payLists) {
			System.out.println(p);
		}
		//test.removeProduct(pl, new Product(4, 4));
	}

}
